/**
* Copyright (c) 2004-2013 Luciano Xumerle. All rights reserved.
*
* This file is part of gephinfo.
*
* gephinfo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*/

/**
 * COMPILE:
 * javac -classpath jenoware.jar ConvertGenotype.java
 *
 * EXECUTE:
 * java -classpath jenoware.jar:postgresql.jar ConvertGenotype.java csv_file
 *
 * The program open a CSV file with a column id and some columns with genotypes
 * codes as single numbered value.
 * Es. genotype 1 is 1/1, 2 is 1/2 and 3 is 2/2
 *
 * Nota: it works with biallelic genotype.
 * Position 0 is a null or a not valid Genotype.
 *
 */

// import jenoware packages
import it.ciano.jenoware.*;
import it.ciano.util.*;

public class ConvertGenotype {
    // the PairAllele array used to convert a biallelic genotype
    private static PairAllele[] pairs = new PairAllele[] {
        new PairAllele( ),
        new PairAllele ( 1, 1 ),
        new PairAllele ( 1, 2 ),
        new PairAllele ( 2, 2 )
    };

    public ConvertGenotype() {
    }

    public static void main ( String[] args )
    throws java.io.IOException {

        // the Pedigree object
        Pedigree ped = new Pedigree();
        // add the CSV files to the Pedigree
        for ( int i = 0; i < args.length; i++ )
            toPedigree.importCSV ( ped, args[ i ], ";" );
        // get ID list
        Sbj[] myID = ped.getListSbj();
        // genotype stored as a single column are detected as phenotype
        String[] phnt = ped.getPhenotypesList();
        if ( phnt != null ) {
            // if exist some phenotypes the process all the Sbj
            for ( int i = 0; i < myID.length; i++ )
                for ( int j = 0; j < phnt.length; j++ ) {
                    // add the new marker to the marker list
                    ped.addMarker2List ( phnt[ j ] );
                    // get the marker position inside the PairAllele array
                    int position = getInt ( myID[ i ].getPhenotype ( phnt[ j ] ).getValue(), pairs.length - 1 );
                    // create the corresponding Genotype
                    Genotype geno = new Genotype ( phnt[ j ], position, pairs, "" );
                    // add the Genotype to the current Sbj
                    myID[ i ].addGenotype ( geno );
                }
        }
        // print result
        System.out.print ( ( new GetCSV ( ped, ";", true, false ) ).toString() );

    } // end main

    private static int getInt ( String s, int max ) {
        Integer ii = CString.getInteger ( s );
        if ( ii == null )
            return 0;
        int yy = ii.intValue();
        if ( yy > max || yy < 0 )
            return 0;
        return yy;
    }

} //end class

